package com.djinfo.work.service.impl;

import com.djinfo.work.common.bean.AjaxJson;
import com.djinfo.work.common.service.impl.BaseServiceImpl;
import com.djinfo.work.dao.QsQuestionDao;
import com.djinfo.work.dao.QsResultDao;
import com.djinfo.work.dao.QsUserDao;
import com.djinfo.work.dao.impl.QsResultDaolmpl;
import com.djinfo.work.entity.QsQuestionEntity;
import com.djinfo.work.entity.QsResultEntity;
import com.djinfo.work.entity.QsUserEntity;
import com.djinfo.work.service.QsResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service("qsResultService")
public class QsResultServiceImpl extends BaseServiceImpl<QsResultEntity,QsResultDaolmpl> implements QsResultService {
	@Autowired
	private QsResultDao qsResultDao;
	@Autowired
	private QsQuestionDao qsQuestionDao;
	@Autowired
	private QsUserDao qsUserDao;


	@Override
	public void save(QsResultEntity object) {
		qsResultDao.save(object);
	}

	@Override
	public void update(QsResultEntity object) {
		qsResultDao.update(object);
	}

	@Override
	public List<QsResultEntity> page(QsResultEntity object, Integer pageNum, Integer pageSize) {
		List list=qsResultDao.page(object,pageNum,pageSize);
		return list;
	}

	@Override
	public Integer getAllRowCount(QsResultEntity object) {
		return qsResultDao.getRowCount(object);
	}

	@Override
	public void delete(String id) {
		qsResultDao.delete(id);
	}

	@Override
	public QsResultEntity selectOne(String id) {
		return qsResultDao.selectOne(id);
	}

	@Override
	public AjaxJson list(QsResultEntity qsResultEntity, Integer pagenum, Integer pagesize) {
		AjaxJson ajaxJson=new AjaxJson();
		try {
			HashMap map=new HashMap();
			List list2=new ArrayList();
			List<QsResultEntity> list=qsResultDao.page(qsResultEntity,pagenum,pagesize);
			for(QsResultEntity qsResultEntity1:list){
                QsQuestionEntity qsQuestionEntity=qsQuestionDao.getById(qsResultEntity1.getQuestionId());
                if(qsQuestionEntity!=null){
                    qsResultEntity1.setQuestionName(qsQuestionEntity.getName());
                }
                QsUserEntity qsUserEntity=qsUserDao.getById(qsResultEntity1.getUserId());
                if(qsUserEntity!=null){
                    qsResultEntity1.setUsername(qsUserEntity.getpName());
                }
                list2.add(qsResultEntity1);
            }
			int count=qsResultDao.getRowCount(qsResultEntity);
			map.put("dataList",list2);
			map.put("total",count);
			ajaxJson.setResult(map);
		} catch (Exception e) {
			ajaxJson.setCode(201);
			ajaxJson.setMsg("error");
			e.printStackTrace();
		}
		return ajaxJson;
	}

	@Override
	public QsResultEntity getQsresultEntityByUidAndContentId(String uid, String contentId) {
		String hql="from QsResultEntity where contentId=? and userId=?";
		List<QsResultEntity> list=qsResultDao.getList(hql,uid,contentId);
		if(list.size()==0){
			return null;
		}else {
			return list.get(0);
		}
	}

	@Override
	public void deleteResultByUserIdAndQuestionId(String userId, String questionId,String isAttached) {
		String hql=" delete FROM  QsResultEntity where userId = ? and questionId=? and isAttached=? ";
		qsResultDao.deleteResultByUserIdAndQuestionId(hql,userId,questionId,isAttached);
	}

	@Override
	public List getQuResultListByUidAndQid(String quId, String questionId) {
		String hql="from QsResultEntity where userId=? and questionId=?";
		List<QsResultEntity> list=qsResultDao.getList(hql,quId,questionId);
		return list;
	}



	@Override
	public QsResultEntity getQsResultByQsId(String qsId) {

		QsResultEntity qsResultEntity=new QsResultEntity();
		String hql=" from QsResultEntity where qsId='"+qsId+"'";
		List<QsResultEntity> list=qsResultDao.getQsResultListByQsId(hql);
		if(list.size()!=0){
			qsResultEntity=list.get(0);
		}
		return qsResultEntity;
	}

}
